<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-14
 * Time: 16:33
 */

namespace app\api\service\partner;


use app\api\model\Assistant;
use app\api\model\City;
use app\api\model\CityPartner;
use app\api\model\CountyPartner;
use app\api\model\Member;
use app\api\model\ProvincePartner;
use app\api\model\User;
use app\api\service\Token;
use app\lib\exception\ParameterException;

class CreateCitySer extends Token
{
    // 选择的城市名称
    protected $city;

    // 选择的城市名称
    protected $province;

    // 用户id
    protected $uid;

    // 会员信息
    protected  $member;

    function __construct($province,$city)
    {
        $this->city = $city;
        $this->province = $province;
    }

    public function set()
    {
        return $this->start();
    }

    // 开始信息
    protected function start()
    {
        self::judgeMember();
        $this->uid = self::getCurrentUid();
        $this->member = Member::where('user_id',$this->uid)->find();
        return $this->judge();
    }

    // 判断该用户是否是合伙人
    protected function judge()
    {
        $provincePartner = ProvincePartner::where('user_id',$this->uid)->find();
        if($provincePartner) {
            if($provincePartner['status'] == 1) {
                throw new ParameterException([
                    'msg' => [
                        'partner' => '您的省级合伙人身份正在审核'
                    ]
                ]);
            } else {
                throw new ParameterException([
                    'msg' => [
                        'partner' => '您已经是省级合伙人'
                    ]
                ]);
            }
        }
        $cityPartner = CityPartner::where('user_id',$this->uid)->find();
        if($cityPartner) {
            if($cityPartner['status'] == 1) {
                throw new ParameterException([
                    'msg' => [
                        'partner' => '您的市级合伙人身份正在审核'
                    ]
                ]);
            } else {
                throw new ParameterException([
                    'msg' => [
                        'partner' => '您已经是市级合伙人'
                    ]
                ]);
            }
        }
        $countPartner = CountyPartner::where('user_id',$this->uid)->find();
        if($countPartner) {
            if($countPartner['status'] == 1) {
                throw new ParameterException([
                    'msg' => [
                        'partner' => '您的区县合伙人身份正在审核'
                    ]
                ]);
            } else {
                throw new ParameterException([
                    'msg' => [
                        'partner' => '您已经是区县合伙人'
                    ]
                ]);
            }
        }
        $assistant = Assistant::where('user_id',$this->uid)->find();
        if($assistant) {
            throw new ParameterException([
                'msg' => [
                    'partner' => '您已经是助理'
                ]
            ]);
        }
        $city = City::where('name',$this->city)->find();
        if($city) {
            $provincePartner = CityPartner::where('city_id',$city['id'])->find();
            if($provincePartner) {
                throw new ParameterException([
                    'msg' => [
                        'partner' => '该城市已有合伙人'
                    ]
                ]);
            }
            CityPartner::create([
                'city' => $this->city,
                'province' => $this->province,
                'user_id' => $this->uid,
                'city_id' => $city['id'],
                'province_id' => $city['province_id'],
                'name' => $this->member['name'],
                'phone' => $this->member['phone'],
                'id_card' => $this->member['id_card']
            ]);
            User::where('id',$this->uid)->update([
                'partner_status' => 2
            ]);
        } else {
            CityPartner::create([
                'city' => $this->city,
                'province' => $this->province,
                'user_id' => $this->uid,
                'name' => $this->member['name'],
                'phone' => $this->member['phone'],
                'id_card' => $this->member['id_card']
            ]);
            User::where('id',$this->uid)->update([
                'partner_status' => 2
            ]);
        }
        return writeJson(201, [], '申请成功！');
    }
}